/*
===========================================================================
This source file is part of DyLab (Dynamic Laboratory)
For the latest info, see http://dylab.googlecode.com

Copyright (c) 2006-2008 Lukas Krejci
(krejci.lukas@volny.cz)

This file is part of DyLab.

    DyLab is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    DyLab is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with DyLab.  If not, see <http://www.gnu.org/licenses/>.
===========================================================================
*/

#ifndef __dylab_DynamicModule_h__
#define __dylab_DynamicModule_h__

//===========================================================================================
#include <dylab/utils/String.h>

//===========================================================================================
namespace dylab {

#if defined(DYLAB_PLATFORM_WINDOWS)
#   define DYLAB_DYNMODULE_HANDLE           void *
#   define DYLAB_DYNMODULE_SUFFIX           ".dll"
#   define DYLAB_DYNMODULE_LOAD(name)       LoadLibrary(name)
#   define DYLAB_DYNMODULE_UNLOAD(h)        !FreeLibrary(h)
#   define DYLAB_DYNMODULE_GETSYMBOL(h, s)  GetProcAddress(h, s)

#elif defined(DYLAB_PLATFORM_LINUX)
#    define DYLAB_DYNMODULE_HANDLE           void *
#    define DYLAB_DYNMODULE_SUFFIX           ".so"
#    define DYLAB_DYNMODULE_LOAD(name)       dlopen(name, RTLD_LAZY | RTLD_GLOBAL)
#    define DYLAB_DYNMODULE_UNLOAD(h)        dlclose(h)
#    define DYLAB_DYNMODULE_GETSYMBOL(h, s)  dlsym(h, s)

#else
#   error "This platform is not supported so far."

#endif

    /** 
     * 
     */    
    class DYLAB_EXPORT DynamicModule
    {
    private:
        /// Handle of the loaded module - its type dependent on the current platform used.
        DYLAB_DYNMODULE_HANDLE mHandle;

        /// The name of the dynamic module - without extension (suffix)!
        String mName;

    public:
        /** 
        @param[in] _name 
			The name of the dynamic module - without extension (suffix)!
        */    
        DynamicModule(const String & _name);

        /** 
         * 
         */    
        virtual ~DynamicModule();

        /** 
         * 
         */    
        void load();

        /** 
         * 
         */    
        void release();

        /** 
         * 
         */    
        void * getSymbol(const String & _symbol);
    };
}
//===========================================================================================
//
#define DYLAB_DYNMODULE_CALL(m, f) static_cast<f>( m->getSymbol(#f) )

//===========================================================================================
#endif // __dylab_DynamicModule_h__
